package com.epam.photographer.DAO;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.epam.photographer.model.Album;
import com.epam.photographer.model.PhotoWrapper;
import com.epam.photographer.model.Photographer;

@Repository
public class DAOImpl implements IDao {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void addPhotographer(Photographer photographer) {
		sessionFactory.getCurrentSession().save(photographer);

	}

	@Override
	public Photographer getPhotographerById(int id) {
		Photographer photographer = (Photographer) sessionFactory
				.getCurrentSession().load(Photographer.class, id);
		return photographer;
	}

	@Override
	public void addPhotoWrapper(PhotoWrapper photo) {
		sessionFactory.getCurrentSession().save(photo);

	}

	@Override
	public PhotoWrapper getPhotoWrapperById(int id) {
		PhotoWrapper photoWrapper = (PhotoWrapper) sessionFactory
				.getCurrentSession().load(PhotoWrapper.class, id);
		return photoWrapper;
	}

	@Override
	public void addAlbum(Album album) {
		sessionFactory.getCurrentSession().save(album);

	}

	@Override
	public Album getAlbumById(int id) {
		Album album = (Album) sessionFactory.getCurrentSession().load(
				Album.class, id);
		return album;
	}

	@SuppressWarnings("unchecked")
	public List<Photographer> getPhotographersList() {
		return sessionFactory.getCurrentSession()
				.createQuery("from Photographers").list();
	}

	@SuppressWarnings("unchecked")
	public List<PhotoWrapper> getPhotoWrappersList() {
		return sessionFactory.getCurrentSession()
				.createQuery("from PhotoWrapper").list();
	}

	@SuppressWarnings("unchecked")
	public List<Album> getAlbumsList() {
		return sessionFactory.getCurrentSession().createQuery("from Album")
				.list();
	}

}
